/**
 * @file ble_config.c
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2023-12-28
 *
 * @copyright Copyright (c) 2023
 *
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aiio_log.h"
#include "aiio_adapter_include.h"
#include "ble_config.h"
#include "aiio_ble_config.h"
#include "cloud_config.h"
#include "staWiFiConnect.h"
#include "cloud_mutual.h"
int ble_config_start = 0;

wifi_config_data_t wifi_config_data = { 0 };

/**
 * @brief aiio_ble_iot_config_cb
 *   爱星云配网回调
 * @param ble_data
*/
static void ble_iot_config_cb(aiio_ble_iot_data_t* ble_data)
{
    switch (ble_data->event)
    {
        case AIIO_BLE_CONFIG_FAIL:                                                  /* The BLE distribution network failed, and the failed event was sent through the queue */
        {
            aiio_log_i("AIIO_BLE_CONFIG_FAIL \r\n");
        }
        break;

        case AIIO_BLE_DISCONNECED:
        {
            aiio_log_i("AIIO_BLE_DISCONNECED \r\n");

        }
        break;

        case AIIO_BLE_CONFIG_OK:                                                    /*The BLE distribution network is successful, and the successful events and distribution network data are sent through the queue */
        {
            aiio_log_i("AIIO_BLE_CONFIG_OK");
            if (ble_data->ble_data->ssid)
            {
                aiio_log_d("ssid = %s ", ble_data->ble_data->ssid);
            }
            if (ble_data->ble_data->passwd)
            {
                aiio_log_d("passwd = %s", ble_data->ble_data->passwd);
            }
            if (ble_data->ble_data->mqttip)
            {
                aiio_log_d("mqttip = %s ", ble_data->ble_data->mqttip);
            }

            memset(&wifi_config_data, 0, sizeof(wifi_config_data));
            /*************    复制连接信息        ********/
            strcpy(wifi_config_data.ssid, ble_data->ble_data->ssid);
            strcpy(wifi_config_data.password, ble_data->ble_data->passwd);
            strcpy(wifi_config_data.token, ble_data->ble_data->token);
            strcpy(wifi_config_data.mqttip, ble_data->ble_data->mqttip);
            strcpy(wifi_config_data.wificc, ble_data->ble_data->wificc);
            strcpy(wifi_config_data.tz, ble_data->ble_data->tz);

            wifi_config_data.port = ble_data->ble_data->port;
            wifi_config_data.ts = ble_data->ble_data->ts;

            cloud_data_s.device_token = wifi_config_data.token;
            cloud_data_s.mqtt_host = wifi_config_data.mqttip;
            cloud_data_s.mqtt_port = wifi_config_data.port;
            //开始连接路由
            staWiFiConnect(ble_data->ble_data->ssid, ble_data->ble_data->passwd);
        }
        break;

        default:
            aiio_log_e("not find event ");
            break;
    }
}

/**
 * @brief 启动配网
 *
*/
void bleConfigStart(void)
{
    aiio_ble_iot_config_t ble_iot_config = { 0 };

    ble_iot_config.deviceid = DEVICE_ID;
    ble_iot_config.productid = PRODUCT_ID;
    ble_iot_config.wifi_flag = PRODUCT_FLAG;
    ble_iot_config.aiio_ble_iot_config_cb = ble_iot_config_cb;
    aiio_ble_config_init(&ble_iot_config);
    ble_config_start = 1;
    cloudConnectDeinit();
}
/**
 * @brief 停止配网
 *
*/
void bleConfigStop(void)
{
    aiio_log_i("aiio_ble_config_stop \r\n");
    aiio_ble_config_deinit();
    ble_config_start = 0;
}
